package ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.DemandListBean;
import com.fy.fyzf.bean.DemanddetailBean;
import com.fy.fyzf.bean.IsInfoBean;
import com.fy.fyzf.bean.IssueDemandBean;
import com.fy.fyzf.bean.MyRecommendBean;
import com.fy.fyzf.dialog.AreaPopwindow;
import com.fy.fyzf.dialog.LoopViewPopwindow;
import com.fy.fyzf.presenter.DemandPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.PhoneFormatCheckUtils;
import com.fy.fyzf.view.DemandCallBack;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.adapter.AreaListAdapter;

//发布需求
public class IssueDemandActivity extends BaseActivity<DemandPresenter> implements DemandCallBack {

    @BindView(R.id.tv_housing_type)
    TextView tvHousingType;
    @BindView(R.id.rl_type)
    RelativeLayout rlType;
    @BindView(R.id.tv_housing_area)
    TextView tvHousingArea;
    @BindView(R.id.rl_area)
    RelativeLayout rlArea;
    @BindView(R.id.et_area_start)
    EditText etAreaStart;
    @BindView(R.id.et_area_end)
    EditText etAreaEnd;
    @BindView(R.id.et_bugget_start)
    EditText etBuggetStart;
    @BindView(R.id.et_bugget_end)
    EditText etBuggetEnd;
    @BindView(R.id.tv_fitment)
    TextView tvFitment;
    @BindView(R.id.rl_fitment)
    RelativeLayout rlFitment;
    @BindView(R.id.tv_schene)
    TextView tvSchene;
    @BindView(R.id.rl_scheme)
    RelativeLayout rlScheme;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_tel)
    EditText etTel;

    private IssueDemandBean issueDemandBean = new IssueDemandBean();
    private RecyclerView recyclerView;
    private AreaListAdapter areaListAdapter;
    private AreaPopwindow areaPopwindow;
    private DemandListBean.ListBean data;

    @Override
    protected DemandPresenter createPresenter() {
        return new DemandPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_issue_demand;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("发布需求");

    }

    @Override
    public void initData() {
        super.initData();
        data = (DemandListBean.ListBean) getIntent().getSerializableExtra("data");

        issueDemandBean.setReleaseType(1);
        issueDemandBean.setCooperationProgram(5);
        if (data != null) {
            showData();
        }
    }

    private void showData() {
        etName.setText(data.getCustomerName());
        etTel.setText(data.getCustomerMobile());
        tvHousingArea.setText(data.getAreaName());
        issueDemandBean.setAreaId(data.getAreaId());
        issueDemandBean.setReleaseType(data.getReleaseType());
        issueDemandBean.setReleaseId(data.getReleaseId());
        switch (data.getReleaseType()) {
            case 1:
                tvHousingType.setText("写字楼");
                break;
            case 2:
                tvHousingType.setText("商铺");
                break;
            case 3:
                tvHousingType.setText("厂房");
                break;
            case 4:
                tvHousingType.setText("酒店");
                break;
            case 5:
                tvHousingType.setText("公寓");
                break;
        }
        tvHousingArea.setText(data.getAreaName());
        issueDemandBean.setAreaId(data.getAreaId());
        String[] split = data.getReleaseArea().split("-");

        etAreaStart.setText(split[0]);
        etAreaEnd.setText(split[1]);
        String[] split1 = data.getReleaseBudget().split("-");
        etBuggetStart.setText(split1[0]);
        etBuggetEnd.setText(split1[1]);
        issueDemandBean.setRenovationType(data.getRenovationType());
        issueDemandBean.setCooperationProgram(data.getCooperationProgram());
        switch (data.getRenovationType()) {
            case 1:
                tvFitment.setText("简装");
                break;
            case 2:
                tvFitment.setText("精装");
                break;
            case 3:
                tvFitment.setText("豪装");
                break;
            case 4:
                tvFitment.setText("无装修");
                break;
        }
        switch (data.getCooperationProgram()) {
            case 1:
                tvSchene.setText("10%");
                break;
            case 2:
                tvSchene.setText("20%");
                break;
            case 3:
                tvSchene.setText("30%");
                break;
            case 4:
                tvSchene.setText("40%");
                break;
            case 5:
                tvSchene.setText("50%");
                break;
        }
        etRemark.setText(data.getOtherNeeds());
//        tvFitment.setText(da);

    }

    private List<String> listStr;
    private List<Integer> listId;

    @OnClick({R.id.rl_type, R.id.rl_area, R.id.rl_fitment, R.id.rl_scheme, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_type:
                listStr = new ArrayList<>();
                listId = new ArrayList<>();

                listId.add(1);
                listId.add(2);
                listId.add(3);
                listId.add(4);
                listId.add(5);
                listStr.add("写字楼");
                listStr.add("商铺");
                listStr.add("厂房");
                listStr.add("酒店");
                listStr.add("公寓");

                selectValue(listStr, listId, 1, tvHousingType);
                break;
            case R.id.rl_area:
                showAreaPop();
                break;
            case R.id.rl_fitment:
                listStr = new ArrayList<>();
                listId = new ArrayList<>();

                listId.add(1);
                listId.add(2);
                listId.add(3);
                listId.add(4);
                listStr.add("简装");
                listStr.add("精装修");
                listStr.add("豪装");
                listStr.add("无装修");

                selectValue(listStr, listId, 3, tvFitment);
                break;
            case R.id.rl_scheme:

                listStr = new ArrayList<>();
                listId = new ArrayList<>();

                listId.add(1);
                listId.add(2);
                listId.add(3);
                listId.add(4);
                listId.add(5);
                listStr.add("10%");
                listStr.add("20%");
                listStr.add("30%");
                listStr.add("40%");
                listStr.add("50%");

                selectValue(listStr, listId, 4, tvSchene);
                break;
            case R.id.tv_ok:
                if (TextUtils.isEmpty(etName.getText().toString()) || TextUtils.isEmpty(etAreaEnd.getText().toString())) {
                    AppUtils.showToast("电话或姓名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etTel.getText().toString()) || TextUtils.isEmpty(etAreaEnd.getText().toString())) {
                    AppUtils.showToast("请输入电话");
                    return;
                }
                if (TextUtils.isEmpty(etAreaStart.getText().toString()) || TextUtils.isEmpty(etAreaEnd.getText().toString())) {
                    AppUtils.showToast("请输入面积");
                    return;
                }

                if (TextUtils.isEmpty(etBuggetStart.getText().toString()) || TextUtils.isEmpty(etBuggetEnd.getText().toString())) {
                    AppUtils.showToast("请输入预算");
                    return;
                }

                if (new BigDecimal(etAreaStart.getText().toString()).subtract(new BigDecimal(etAreaEnd.getText().toString())).doubleValue() >0) {
                    AppUtils.showToast("面积区间不正确");
                    return;
                }

                if (new BigDecimal(etBuggetStart.getText().toString()).subtract(new BigDecimal(etBuggetEnd.getText().toString())).doubleValue() >0) {
                    AppUtils.showToast("预算价格区间不正确");
                    return;
                }
                if (TextUtils.isEmpty(etName.getText().toString())) {
                    AppUtils.showToast("请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(etTel.getText().toString())) {
                    AppUtils.showToast("请输入电话");
                    return;
                }

                if (!PhoneFormatCheckUtils.isChinaPhoneLegal(etTel.getText().toString())) {
                    AppUtils.showToast("手机号不合法");
                    return;
                }
                if (TextUtils.isEmpty(tvHousingType.getText().toString())) {
                    AppUtils.showToast("请选择类型");
                    return;
                }
                if (TextUtils.isEmpty(tvHousingArea.getText().toString())) {
                    AppUtils.showToast("请选择区域");
                    return;
                }

                if (TextUtils.isEmpty(tvFitment.getText().toString())) {
                    AppUtils.showToast("请选择装修情况");
                    return;
                }

                if (TextUtils.isEmpty(tvSchene.getText().toString())) {
                    AppUtils.showToast("请选择装合作方案");
                    return;
                }
                issueDemandBean.setCustomerName(etName.getText().toString());
                issueDemandBean.setCustomerMobile(etTel.getText().toString());
                issueDemandBean.setOtherNeeds(etRemark.getText().toString());
                issueDemandBean.setReleaseAreaMin(etAreaStart.getText().toString());
                issueDemandBean.setReleaseAreaMax(etAreaEnd.getText().toString());
                issueDemandBean.setReleaseBudgetMin(etBuggetStart.getText().toString());
                issueDemandBean.setReleaseBudgetMax(etBuggetEnd.getText().toString());

                Log.e("需求数据", new Gson().toJson(issueDemandBean));
                if (data == null) {
                    mPresenter.issueDemand(issueDemandBean);
                } else {
                    mPresenter.editDemand(issueDemandBean);
                }
                break;
        }
    }

    private int flag = 1;
    private Integer parentId = 16;
    private int selectPos = 0;
    private String areaName = "";

    public void showAreaName() {
        tvHousingArea.setText(areaName);

    }

    private Integer firstAreaId;
    private void showAreaPop() {
        flag = 1;
        count = 1;
        areaName = "";
        parentId = 16;
        areaPopwindow = new AreaPopwindow(this).setView(tvHousingType);
        recyclerView = (RecyclerView) areaPopwindow.getView(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        areaListAdapter = new AreaListAdapter();
        recyclerView.setAdapter(areaListAdapter);
        areaPopwindow.showPop();
//        areaPopwindow.

        areaListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                selectPos = position;
                if (count == 1 || count == 2) {
                    flag = 1;
                } else if (count == 3) {
                    flag = 2;
                } else {
                    flag = 3;
                }
//                flag++;
                List<AreaListBean> data = adapter.getData();
                areaName = areaName + data.get(position).getName();
                parentId = data.get(position).getId();
                issueDemandBean.setAreaId(parentId);
                areaListAdapter.setSelect(position);
                if (flag == 2 || position  == 0) {
                    areaPopwindow.disMiss();
//                    issueDemandBean.setAreaId(null);
                }


                if (flag == 1 ) {
                    firstAreaId = data.get(position).getId();
                    if (position == 0) {
                        issueDemandBean.setAreaId(16);
                    }
                }else if(flag == 2){
                    if (position == 0) {
                        issueDemandBean.setAreaId(firstAreaId);
                    }
                }
                mPresenter.getAreaData(flag, parentId);
            }
        });
        mPresenter.getAreaData(flag, parentId);
    }

    /**
     * @param titles
     * @param idS
     * @param type     1 发布类型 2 区域 3 装修 4合作方案
     * @param textView
     */
    public void selectValue(List<String> titles, List<Integer> idS, int type, TextView textView) {
        LoopViewPopwindow loopViewPopwindow = new LoopViewPopwindow(this);
        loopViewPopwindow.setIds(idS).setTitleData(titles).setTitle(" ").showPop(rlArea);
        loopViewPopwindow.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(titles.get(loopViewPopwindow.position));
                loopViewPopwindow.dismiss();
                if (type == 1) {
                    issueDemandBean.setReleaseType(idS.get(loopViewPopwindow.position));
                } else if (type == 2) {

                } else if (type == 3) {
                    issueDemandBean.setRenovationType(idS.get(loopViewPopwindow.position));
                } else {
                    issueDemandBean.setCooperationProgram(idS.get(loopViewPopwindow.position));
                }
            }
        });
    }

    @Override
    public void getDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void issueDemandSuccess(BaseData data) {
        AppUtils.showToast("发布成功");
        finish();
        EventBus.getDefault().post("issueSuccess");
    }

    private int count = 1;

    @Override
    public void getAreaListSuccess(List<AreaListBean> listBeans) {
        count++;
        if (listBeans.size() == 0) {
            areaPopwindow.disMiss();
            tvHousingArea.setText(listBeans.get(selectPos).getAreaName());
        }
        listBeans.add(0,new AreaListBean(null,"全部","全部"));
        areaListAdapter.setNewData(listBeans);
    }

    @Override
    public void getDamendDetailSuccess(DemanddetailBean demanddetailBean) {

    }

    @Override
    public void getMyDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void deleteDemandSuccess(BaseData data, int i) {

    }

    @Override
    public void getMyRecommendSuccess(MyRecommendBean myRecommendBean) {

    }

    @Override
    public void isInfoSuccess(IsInfoBean isInfoBean) {

    }
}